// components/buyCat/buyCat.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goods: {
      type: Array,
      value: []
    },
    goodCat: {
      type: String,
      value: ''
    },
    goodCatNum: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    goodList: [],
    vNum: 0,
    mNum: 0,
    eNum: 0,
    bbNum: 0,
    fNum: 0,
    bNum: 0,
    rNum: 0,
    cNum: 0,
    ffNum: 0,
    mmNum: 0,
    fffNum: 0,
    limitNum: 0,
    displayNum: '',
    isClick:true
  },
  lifetimes: {
    ready: function () {
      this.toSum()
      // 获取进入页面各种类的总数
      let vegNum = 0
      let meeNum = 0
      let eggNum = 0
      let birNum = 0
      let fisNum = 0
      let besNum = 0
      let ricNum = 0
      let corNum = 0
      let fruNum = 0
      let mliNum = 0
      let freNum = 0

      for (let i = 0; i < this.data.goodList.length; i++) {
        if (this.data.goodList[i].dishType === '蔬菜') {
          vegNum += this.data.goodList[i].buyNum
          this.setData({
            vNum: vegNum
          })
        }
        if (this.data.goodList[i].dishType === '肉') {
          meeNum += this.data.goodList[i].buyNum
          this.setData({
            mNum: meeNum
          })
        }
        if (this.data.goodList[i].dishType === '蛋') {
          eggNum += this.data.goodList[i].buyNum
          this.setData({
            eNum: eggNum
          })
        }
        if (this.data.goodList[i].dishType === '禽类') {
          birNum += this.data.goodList[i].buyNum
          this.setData({
            bbNum: birNum
          })
        }
        if (this.data.goodList[i].dishType === '水产') {
          fisNum += this.data.goodList[i].buyNum
          this.setData({
            fNum: fisNum
          })
        }
        if (this.data.goodList[i].dishType === '特产') {
          besNum += this.data.goodList[i].buyNum
          this.setData({
            bNum: besNum
          })
        }
        if (this.data.goodList[i].dishType === '大米') {
          ricNum += this.data.goodList[i].buyNum
          this.setData({
            rNum: ricNum
          })
        }
        if (this.data.goodList[i].dishType === '玉米') {
          corNum += this.data.goodList[i].buyNum
          this.setData({
            cNum: corNum
          })
        }
        if (this.data.goodList[i].dishType === '水果') {
          fruNum += this.data.goodList[i].buyNum
          this.setData({
            ffNum: fruNum
          })
        }
        if (this.data.goodList[i].dishType === '畜肉') {
          mliNum += this.data.goodList[i].buyNum
          this.setData({
            mmNum: mliNum
          })
          console.log(mmNum)
        }
        if (this.data.goodList[i].dishType === '赠品') {
          freNum += this.data.goodList[i].buyNum
          this.setData({
            fffNum: freNum
          })
        }
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 分类开关
    closeOpen(){
      this.setData({
        isClick:!this.data.isClick
      })
    },
    // -按钮
    ddaNum(e) {
      let value = e.currentTarget.dataset;
      let buyNum = value.buynum.buyNum
      let temporaryNum = 0
      let availableQuantity = value.buynum.availableQuantity
      let totalInventory = value.buynum.totalInventory
      let goods = this.data.goodList
      let dishType = value.buynum.dishType
      const index = value.index
      if (parseInt(buyNum) > 0) {
        buyNum -= 1
        temporaryNum = 0
        temporaryNum += 1
        if (dishType === '蔬菜' && this.data.vNum !== 0) {
          this.setData({
            vNum: this.data.vNum - 1
          })
        }
        if (dishType === '肉' && this.data.mNum !== 0) {
          this.setData({
            mNum: this.data.mNum - 1
          })
        }
        if (dishType === '蛋' && this.data.eNum !== 0) {
          this.setData({
            eNum: this.data.eNum - 1
          })
        }
        if (dishType === '禽类' && this.data.bbNum !== 0) {
          this.setData({
            bbNum: this.data.bbNum - 1
          })
        }
        if (dishType === '水产' && this.data.fNum !== 0) {
          this.setData({
            fNum: this.data.fNum - 1
          })
        }
        if (dishType === '特产' && this.data.bNum !== 0) {
          this.setData({
            bNum: this.data.bNum - 1
          })
        }
        if (dishType === '大米' && this.data.rNum !== 0) {
          this.setData({
            rNum: this.data.rNum - 1
          })
        }
        if (dishType === '玉米' && this.data.cNum !== 0) {
          this.setData({
            cNum: this.data.cNum - 1
          })
        }
        if (dishType === '水果' && this.data.ffNum !== 0) {
          this.setData({
            ffNum: this.data.ffNum - 1
          })
        }
        if (dishType === '畜肉' && this.data.mmNum !== 0) {
          this.setData({
            mmNum: this.data.mmNum - 1
          })
        }
        if (dishType === '赠品' && this.data.fffNum !== 0) {
          this.setData({
            fffNum: this.data.fffNum - 1
          })
        }
      } else temporaryNum = 0

      goods[index].buyNum = buyNum
      goods[index].temporaryNum = temporaryNum
      goods[index].availableQuantity = availableQuantity + temporaryNum
      goods[index].totalInventory = totalInventory + temporaryNum
      this.setData({
        goodList: goods
      })
      this.triggerEvent('cgnum', {
        good: this.data.goodList,
        goodC: this.data.goodCat,
        goodCatNum: this.data.limitNum
      }, {})
      
    },

    // +按钮
    addNum(e) {
      let value = e.currentTarget.dataset;
      let buyNum = value.buynum.buyNum
      let temporaryNum = 0
      let availableQuantity = value.buynum.availableQuantity
      let totalInventory = value.buynum.totalInventory
      let goods = this.data.goodList
      let dishType = value.buynum.dishType
      const num = value.buynum.sum
      const deliveryNum = value.buynum.availableQuantity
      const index = value.index
      console.log(this.data.limitNum)
      if (buyNum < num && 0 < deliveryNum) {
        if (dishType === '蔬菜' && this.data.vNum < this.data.limitNum) {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
          this.setData({
            vNum: this.data.vNum + 1
          })
        }
        if (dishType === '肉' && this.data.mNum < this.data.limitNum) {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
          this.setData({
            mNum: this.data.mNum + 1
          })
        }
        if (dishType === '蛋' && this.data.eNum < this.data.limitNum) {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
          this.setData({
            eNum: this.data.eNum + 1
          })
        }
        if (dishType === '禽类' && this.data.bbNum < this.data.limitNum) {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
          this.setData({
            bbNum: this.data.bbNum + 1
          })
        }
        if (dishType === '水产' && this.data.fNum < this.data.limitNum) {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
          this.setData({
            fNum: this.data.fNum + 1
          })
        }
        if (dishType === '特产' && this.data.bNum < this.data.limitNum) {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
          this.setData({
            bNum: this.data.bNum + 1
          })
        }
        if (dishType === '大米' && this.data.rNum < this.data.limitNum) {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
          this.setData({
            rNum: this.data.rNum + 1
          })
        }
        if (dishType === '玉米' && this.data.cNum < this.data.limitNum) {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
          this.setData({
            cNum: this.data.cNum + 1
          })
        }
        if (dishType === '水果' && this.data.ffNum < this.data.limitNum) {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
          this.setData({
            ffNum: this.data.ffNum + 1
          })
        }
        if (dishType === '畜肉' && this.data.mmNum < this.data.limitNum) {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
          this.setData({
            mmNum: this.data.mmNum + 1
          })
        }
        if (dishType === '赠品' && this.data.fffNum < this.data.limitNum) {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
          this.setData({
            fffNum: this.data.fffNum + 1
          })
        }
        if (dishType !== '蔬菜' && dishType !== '蛋' && dishType !== '禽类' && dishType !== '水产' && dishType !== '特产' && dishType !== '肉' && dishType !== '大米' && dishType !== '玉米' && dishType !== '水果' && dishType !== '畜肉' && dishType !== '赠品') {
          temporaryNum = 0
          temporaryNum -= 1
          buyNum += 1
        }
      }
      goods[index].buyNum = buyNum
      goods[index].temporaryNum = temporaryNum
      goods[index].availableQuantity = availableQuantity + temporaryNum
      goods[index].totalInventory = totalInventory + temporaryNum
      this.setData({
        goodList: goods
      })
      this.triggerEvent('cgnum', {
        good: this.data.goodList,
        goodC: this.data.goodCat,
        goodCatNum: this.data.limitNum
      }, {})
      
    },

    // 总数
    toSum: function () {
      const list = [...this.data.goods]
      for (let i = 0; i < list.length; i++) {
        list[i].sum = parseInt(list[i].buyNum) + parseInt(list[i].totalInventory)
        if (list[i].cycle === '每天') list[i].cycle = '天'
        if (list[i].cycle === '每周') list[i].cycle = '周'
        if (list[i].cycle === '每月') list[i].cycle = '月'
        if (list[i].cycle === '每季') list[i].cycle = '季'
        if (list[i].cycle === '每年') list[i].cycle = '年'
      }
      let limitNum = this.data.goodCatNum
      let displayNum = ''
      if (limitNum == '*') {
        limitNum = 99
        displayNum = '任选'
      } else {
        limitNum = parseInt(limitNum)
        displayNum = `选${limitNum}份`
      }
      this.setData({
        goodList: list,
        limitNum,
        displayNum
      })
      console.log(list)
    }
  }
})